package com.shixulin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.util.StringUtils;
import com.shixulin.domain.Message;
import com.shixulin.domain.ResponseResult;
import com.shixulin.entity.Teacher;
import com.shixulin.mapper.TeacherMapper;
import com.shixulin.service.TeacherService;
import com.shixulin.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * (Teacher)表服务实现类
 *
 * @author makejava
 * @since 2023-11-07 16:08:27
 */
@Service("teacherService")
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;


    @Override
    public ResponseResult login(Teacher teacher) {
      Teacher te =  teacherMapper.login(teacher);
      String token = null;
      if(te!=null){
//          QueryWrapper queryWrapper = new QueryWrapper();
//          queryWrapper.eq("id",te.getId());
//          return new ResponseResult(200,"登录成功",teacherMapper.selectOne(queryWrapper));
          token = JwtUtil.createJWT(UUID.randomUUID().toString(),te.getId().toString(),null);
          Map map = new HashMap();
          map.put("token",token);
          return new ResponseResult(200,"登录成功",map);
      }else {
          return new ResponseResult(401,"登录失败");
      }
    }
    @Override
    public ResponseResult getInfo(String username) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        Teacher teacherInfo = teacherMapper.selectOne(queryWrapper);
        if (teacherInfo == null) {
            return new ResponseResult(402,"查无此人");
        }else {
            return new ResponseResult(201,"查询成功",teacherInfo);
        }
    }
    @Override
    public ResponseResult changePassword(Teacher teacher) {
        try{
            int update = teacherMapper.changePassword(teacher);
            if (update > 0) {
                return new ResponseResult(300,"修改成功");
            }else {
                return new ResponseResult(505,"修改失败");
            }

        }catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(406,"错误");
        }

    }
}
